libname mylib 'g:\Dropbox\Wall Street Bets (Private)\Data';


*This program contains the information needed to create Table 6 of the paper;
*The program references the following datasets:

1) daily_panel_rfs. This dataset is constructed in  Daily Panel RFS (Intermediate File) code
2) ibes17_21 (Source: IBES Detail History - Detail Detail File with Actuals 
3) media coverage rank ( Source: Bloomberg);


data flags;
set mylib.daily_panel_rfs;
keep ticker date;
run;


PROC SORT NODUPKEY DATA=MYLIB.ibes17_21 OUT=EARN_DATE;
BY OFTIC ANNDATS_ACT;
RUN;
DATA EARN_DATE;
SET EARN_DATE;
KEEP OFTIC ANNDATS_ACT;
IF OFTIC = '' THEN DELETE;
IF ANNDATS_ACT <0 THEN DELETE;
RUN;

PROC SQL;
CREATE TABLE EARN_FLAG AS
SELECT * 
FROM flags, EARN_DATE
WHERE flags.TICKER = EARN_DATE.OFTIC AND flags.date -1 <=EARN_DATE.ANNDATS_ACT <=flags.date;
QUIT;
DATA EARN_FLAG;
SET EARN_FLAG;
EARN_FLAG =1;
RUN;


PROC SORT NODUPKEY DATA=MYLIB.IBES_detail17_20 OUT=REVISION_DATE;
BY OFTIC ANNDATS;
RUN;
DATA REVISION_DATE;
SET REVISION_DATE;
KEEP OFTIC ANNDATS;
IF OFTIC = '' THEN DELETE;
IF ANNDATS <0 THEN DELETE;
RUN;


PROC SQL;
CREATE TABLE REVISION_FLAG AS
SELECT * 
FROM flags, REVISION_DATE
WHERE flags.TICKER = REVISION_DATE.OFTIC AND flags.date -1 <=REVISION_DATE.ANNDATS <=flags.date;
QUIT;
PROC SORT NODUPKEY DATA=REVISION_FLAG;
BY v1;
RUN;

DATA REVISION_FLAG;
SET REVISION_FLAG;
REVISION_FLAG =1;
RUN;



data news;
set mylib.media_coverage_rank;
date = date2;
keep ticker date news_rank;
run;


proc sort data=flags;
by date ticker;
run;
proc sort data=news;
by date ticker;
run;
proc sort data=EARN_FLAG;
by date ticker;
run;
proc sort data=REVISION_FLAG;
by date ticker;
run;

data flags2;
merge flags news EARN_FLAG revision_flag;
by date ticker;
if news_rank >80 then news_flag =1;
else news_flag =0;
if earn_flag = . then earn_flag =0;
if revision_flag = . then revision_flag =0;
run;

proc corr dat=flags2;
var news_flag earn_flag revision_flag;
run;





data info;
set mylib.daily_panel_rfs;

keep ticker date cum_month gme_amc_flag post_gme
ret1 ret5 ret21
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 SENTIMENT21 total_dd_posts;
run;
proc sort data=flags2;
by date ticker;
run;
data info;
merge info flags2;
by date ticker;
if ret5 = . then delete;
any_flag = max(news_flag, earn_flag, revision_flag);
run;


data info;
set info;

net_dd2_production = net_dd2 * (1-any_flag);
net_dd2_processing =  net_dd2 * any_flag;

net_dd2_production_post = net_dd2_production * post_gme;
net_dd2_processing_post = net_dd2_processing * post_gme;






run;





*The code below reports specification 1 of Table 3 (i.e., 5-day ahead returns and the full sample of stocks).
* To obtain Specification 2, replace Ret5 with Ret 21.
* To obtain Specification 3 impose the filter "where gme_amc_flag  =0" by removing the "*" in the first line of code;
* To obtain Specification 4 replace Ret5 with Ret 21 and impose the filter "where gme_amc_flag  =0" by removing the "*" in the first part of code;



PROC SORT DATA=info;
BY DATE;
RUN;
PROC STANDARD DATA=info OUT=INFO_FE MEAN=0;
BY DATE;
var ret5 ret21
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 SENTIMENT21
net_dd2_production net_dd2_processing net_dd2_production_post net_dd2_processing_post;
*WHERE GME_AMC_FLAG =0;
RUN;




%let yourdata=INFO_FE;

%let firmid=ticker ;
%let time=cum_month;

%let y=ret5 ;
%let x=  net_dd2_processing net_dd2_processing_post net_dd2_production  net_dd2_production_post   NON_RESEARCH2 NON_RESEARCH2_POST NET_SA2 NET_SA2_POST ln_size ln_bm  abn_ret mom5 mom6_26       news_sentiment lag_sent5 lag_sent6_26   BM_MISSING SIZE_MISSING;
RUN;




proc surveyreg data=&yourdata;

    cluster &firmid;
    model &y = &x /covb;
    ods output covb=firm;
run;
quit;


*cluster by second dimension (e.g., year);
proc surveyreg data=&yourdata;

    cluster &time;
    model &y = &x /covb;
    ods output covb=year;
run;
quit;

*cluster by intersection of the two dimensions (e.g, firm-year);
proc surveyreg data=&yourdata;
    cluster &firmid &time;
    model &y = &x /covb;
    ods output covb=both;
    ods output parameterestimates=parm;
run;
quit;

*keeps original parameter estimates;
data parm; set parm;
    keep parameter estimate;
run;

*returns a dataset with a scalar for the dimensions of the var/cov matrix. This is needed to extract the square roots of the diagonals later on;
data parm1; set parm;
    n=_n_;
    m=1;
    keep m n;
run;

data parm1; set parm1; by m;
    if last.m;
    keep n;
run;

*uses matrix algebra interface to construct Var-cov matrix and extract the standard errors;
proc iml;
    use both;
    read all var _num_ into Z;
    print Z;
    use firm;
    read all var _num_ into X;
    print X;
    use year;
    read all var _num_ into Y;
    print Y;
    use parm1;
    read all var _num_ into n;
    print n;

    B=X+Y-Z;
    C=I(n);
    D=J(n,1);
    E=C#B;
    F=E*D;
    G=F##.5;

    print B;
    print G;
    create b from G [colname='stderr']; ;
    append from G;
quit;

*creates a dataset called 'results' that contains the parameter estimates, the SE's, and the t-stats;
data results; merge parm B;
    tstat=estimate/stderr;
run;

proc print data=results;
run;




data spec3;
set results;
estimate3 = estimate  ;
obs = _n_;
keep parameter estimate3 obs;
run;
data spec3b;
set results;
estimate3 = tstat ;
obs = _n_ + .5;
keep parameter estimate3 obs;
run;
data spec3;
set spec3 spec3b;
run;
proc sort data=spec3;
by obs;
run;
